package com.polaris.live.utils.zego

import com.polaris.live.common.util.LogExt

/**
 * 辅助控制推拉流
 * @author Created by 天晴 on 2024/3/12 15:11
 * @since 1.0.0
 **/
object PublishingStreamProxy {
    private const val NO = 0

    //上次推流Id
    private var lastStreamID = ""
    private var lastHashCode: Int = NO
    fun start(hashCode: Int, streamID: String, back: (check: Boolean) -> Unit) {
        if (streamID.isEmpty()) {
            LogExt.logE("PublishingStreamProxy", "streamID  null---------------")
            back.invoke(false)
            return
        }
        if (lastStreamID.isNotEmpty()) {
            LiveEngineManage.getEngine()?.stopPublishingStream()
        }
        lastHashCode = hashCode
        lastStreamID = streamID
        back.invoke(true)
    }

    /**
     * 检查是否可以停止推流
     */
    fun checkState(
        hashCode: Int,
        streamID: String?,
        back: (check: Boolean) -> Unit
    ) {
        //不相同的类型不能关闭
        if (lastHashCode != hashCode) {
            back.invoke(false)
            return
        }
        //没有开启新的推流
        if (lastStreamID.isEmpty()) {
            back.invoke(false)
            return
        }
        if (lastHashCode == hashCode || lastStreamID == streamID) {
            back.invoke(true)
            lastHashCode = NO
            lastStreamID = ""
            return
        }
    }
}
